<template>
  <div class="ma-content-block">
    <!-- Tab 栏 jia-->
    <a-tabs v-model="activeTab" @change="handleTabChange" v-if="false">
      <a-tab-pane key="zh" title="中文"></a-tab-pane>
      <a-tab-pane key="en" title="英文"></a-tab-pane>
      <a-tab-pane key="ja" title="日语"></a-tab-pane>
      <a-tab-pane key="ko" title="韩语"></a-tab-pane>
    </a-tabs>

    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="招聘企业" field="enterprise_name">
            <a-input v-model="searchForm.enterprise_name" placeholder="请输入招聘企业" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="联系人" field="contact_person">
            <a-input v-model="searchForm.contact_person" placeholder="请输入联系人" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="联系电话" field="contact_number">
            <a-input v-model="searchForm.contact_number" placeholder="请输入联系电话" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/enterprise'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  enterprise_name: '',
  contact_person: '',
  contact_number: '',
  lang_code:''
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  pk: 'enterprise_id',
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/recruitmentEnterprise/recruitmentEnterprise/save'],
    func: async (params) => {
      const currentLangCode = searchForm.value.lang_code;
      editRef.value?.open()
      editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/recruitmentEnterprise/recruitmentEnterprise/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/recruitmentEnterprise/recruitmentEnterprise/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
    export: { show: true, url: '/recruitmentEnterprise/recruitmentEnterprise/export', auth: ['/recruitmentEnterprise/recruitmentEnterprise/export'] },
})

// SaTable 列配置
const columns = reactive([
  { title: '招聘企业', dataIndex: 'enterprise_name', width: 180 },
  { title: 'logo', dataIndex: 'institution_logo', type: 'image', width: 180 },
  { title: '企业人数', dataIndex: 'enterprise_size', width: 180 },
  { title: '联系人', dataIndex: 'contact_person', width: 180 },
  { title: '联系电话', dataIndex: 'contact_number', width: 180 },
  { title: '企业地址', dataIndex: 'enterprise_address', width: 180 },
  { title: '更新时间', dataIndex: 'update_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
  searchForm.value.lang_code='zh'
}


//jia
const handleTabChange = async (key) => {
  // 设置语言代码
  searchForm.value.lang_code = key
  // 刷新表格数据
  crudRef.value?.refresh()
}
// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
//jia
const changeSortOrder = async (id,sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: id,
      sort_order: sortOrder
    });
    console.log(response,"fffffffffffffffffff")
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.log(error,"error")
    Message.error('排序修改失败1');
  }
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
